<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-22
 * Time: 14:52
 */

namespace app\src\jsqdb\logic;


use app\src\base\logic\BaseLogic;
use app\src\jsqdb\model\DevList;

class DevListLogic extends BaseLogic
{
    public function _init()
    {
        $this->setModel(new DevList());
    }

    public function generate($no,$cnt = 50,$prefix="D00100001"){
//        D00100000100 XXXX
        //$no = 4位十六进制
        //1.  插入$cnt条数据到数据库中
//        D00100001
//        D00100001000DQB9
//        D00100000014B021
        for($i = 0;$i<$cnt;$i++) {
            $rand = strtoupper($this->generateRand(4));
            $inc  = $this->hexAdd($no,$i);

            $pad_len = 3;

            $inc = str_pad($inc,$pad_len,"0",STR_PAD_LEFT);

            $did = $prefix . $inc . $rand;
            $entity = [
                'did'=>$did,
                'pwd'=>$this->generatePwd(8)
            ];

            $result = $this->add($entity);
        }
    }

    private function hexAdd($no,$i){
        $result = hexdec($no) + ($i);
        return strtoupper(strval(dechex($result))) ;
    }


    private function generateRand($len){
        $str = null;
        $strPol = "ABCDEF0123456789";
        $max = strlen($strPol)-1;

        for($i=0;$i<$len;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $str;
    }
    private function generatePwd($len){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789abcdefghijklmnpqrstuvwxyz";
        $max = strlen($strPol)-1;

        for($i=0;$i<$len;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $str;
    }
}